package com.muyu.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.muyu.entity.PeopleEntity;
import com.muyu.mapper.PeopleMapper;
import com.muyu.service.PeopleService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @ClassName: PeopleServiceImpl
 * @Author: Zirun.Xin
 * @Date: 2025/1/3 18:23
 * @Version: 1.0
 * @Description: 人类信息业务实现层
 */
@Slf4j
@Service
public class PeopleServiceImpl extends ServiceImpl<PeopleMapper, PeopleEntity> implements PeopleService {

    /**
     * 人类信息持久层
     */
    @Autowired
    private PeopleMapper peopleMapper;

    /**
     * 根据性别查询人类信息
     * @param sex 性别
     * @return 查询内容
     */
    @Override
    public List<PeopleEntity> getBySex(String sex) {
        // 条件构造器
        QueryWrapper<PeopleEntity> wrapper = new QueryWrapper<>();
        // eq 代表“ = ”；例如 eq("sex", "男") ---> sex = '男';等同于拼接在sql语句后边的where条件。
        wrapper.eq("sex", sex);
        // 将条件带入查询
        List<PeopleEntity> list = this.list(wrapper);
        // 返回数据
        return list;
    }

    /**
     * 根据ID修改人类信息
     * @param id 主键ID
     * @return 修改是否成功
     */
    @Override
    public boolean updateById(Integer id) {
        // 条件构造器
        UpdateWrapper<PeopleEntity> wrapper = new UpdateWrapper<>();
        // 传入条件
        wrapper.eq("id", id);
        // 调用修改方法
        return this.update(wrapper);
    }

    /**
     * 根据姓名和性别查询人类信息
     * @param name 姓名
     * @param sex 性别
     * @return 查询内容
     */
    @Override
    public List<PeopleEntity> getNameAndSex(String name, String sex) {
        // lambda条件构造器
        LambdaQueryWrapper<PeopleEntity> queryWrapper = new LambdaQueryWrapper<>();
        // 传入条件
        queryWrapper.eq(PeopleEntity::getName, name);
        queryWrapper.eq(PeopleEntity::getSex, sex);
        // 调用查询方法
        return this.list(queryWrapper);
    }
}
